<template>
    <div class="index-question">
      <div class="index-question-table-box">
        <page-container class="left">
          <el-form inline="true" ref="formModel" :model="form">
            <el-form-item style="max-width:864px" class="question-header">
              <el-input v-model="params.keyword" placeholder="请您输入题目标题或内容"
                style="width: 100%; margin: 20px 0 20px 10px;" />
            </el-form-item>
            <el-form-item style="width: 180px;">
              <selector v-model="params.difficulty"></selector>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" plain @click="onSearch">搜索</el-button>
              <el-button type="info" plain @click="onReset">重置</el-button>
            </el-form-item>
          </el-form>
          <!-- 表格 -->
          <el-table :data="questionList" height="580px" style="width: 100%">
            <el-table-column align="center" width="100px" label="序号">
              <template #default="{ $index }">
                {{ (params.pageNum - 1) * params.pageSize + $index + 1 }}
              </template>
            </el-table-column>
            <el-table-column align="left" prop="title" :show-overflow-tooltip="true" label="题目标题" />
            <el-table-column align="center" width="180px" prop="difficulty" label="题目难度">
              <template #default="{ row }">
                <div style="color:#3EC8FF;" v-if="row.difficulty === 1">简单</div>
                <div style="color:#FE7909;" v-if="row.difficulty === 2">中等</div>
                <div style="color:#FD4C40;" v-if="row.difficulty === 3">困难</div>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="180px">
              <template #default="{ row }">
                <el-button type="text" plain v-if="isLogin" @click="goQuestTest(row.questionId)">
                  开始答题
                </el-button>
                <span style="color:#9E9E9E;" v-else>请登录后参与答题</span>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页区域 -->
          <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="total"
            v-model:current-page="params.pageNum" v-model:page-size="params.pageSize" :page-sizes="[5, 10, 15, 20]"
            @size-change="handleSizeChange" @current-change="handleCurrentChange"
            style="margin:20px 100px 20px 0; justify-content: flex-end" />
        </page-container>
        <div class="right">
          <div class="top-box">
            <el-calendar v-model="today"> </el-calendar>
          </div>
          <div class="bot-box">
            <div class="title"><img width="96px" height="24px" src="@/assets/rebang.png" alt=""></div>
            <div class="hot-list">
              <div class="list-item" v-for="(item, index) in hotQuestionList" :key="'hot_' + index" >
                <img class="index-box" v-if="index == 0" src="@/assets/images/icon_1.png" alt="">
                <img class="index-box" v-if="index == 1" src="@/assets/images/icon_2.png" alt="">
                <img class="index-box" v-if="index == 2" src="@/assets/images/icon_3.png" alt="">
                <span class="index-box" v-if="index > 2">{{ index + 1 }}</span>
                <span class="txt" :title="item.title">{{ item.title }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { reactive, ref } from "vue"
  import Selector from "@/components/QuestionSelector.vue"
  import { getQuestionListService,getHotQuestionListService } from '@/apis/question'
  import { getToken } from "@/utils/cookie"
  import router from "@/router"
  import { getUserInfoService } from "@/apis/user"
  
  const params = reactive({
    pageNum: 1,
    pageSize: 10,
    difficulty: '',
    keyword: ''
  })
  let today = ref(new Date()) //日历默认今天
  const questionList = ref([]) //题目列表
  const total = ref(0)
  const isLogin = ref(false)
  
  async function checkLogin() {
    if (getToken()) {
      await getUserInfoService()
      isLogin.value = true
    }
  }
  checkLogin()
  
  async function getQuestionList() {
    const result = await getQuestionListService(params)
    questionList.value = result.rows
    total.value = result.total
  }
  getQuestionList()
  
  const hotQuestionList = ref([])
  async function getHotQuestionList(params) {
    const hotRef = await getHotQuestionListService()
    hotQuestionList.value = hotRef.data
  }
  
  getHotQuestionList()
  
  // 搜索/重置
  function onSearch() {
    params.pageNum = 1
    getQuestionList()
  }
  
  function onReset() {
    params.pageNum = 1
    params.pageSize = 9
    params.difficulty = ''
    params.keyword = ''
    getQuestionList()
  }
  
  // 分页
  function handleSizeChange(newSize) {
    console.log(params.pageSize)
    params.pageNum = 1
    getQuestionList()
  }
  
  function handleCurrentChange(newPage) {
    getQuestionList()
  }
  

  function goQuestTest(questionId) {
    router.push(`/c-oj/anwser?questionId=${questionId}`)
  }
  </script>
  
  <style lang="scss" scoped>
  .index-question {
    background-color: #f8fafc;
    display: flex;
    justify-content: center;
    margin-top: 20px;
    min-height: calc(100vh - 100px);
    padding: 24px;
  
    .index-question-table-box {
      display: flex;
      max-width: 1520px;
      width: 100%;
      justify-content: space-between;
      gap: 24px;
  
      .right {
        width: 340px;
        display: flex;
        flex-direction: column;
        gap: 24px;
  
        :deep(.top-box) {
          width: 100%;
          height: 345px;
          padding: 20px;
          border-radius: 16px;
          background: linear-gradient(164deg, #E2F7FF 0%, #FFFFFF 50%);
          box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
          animation: fadeIn 0.5s ease-out;
  
          .el-calendar {
            background: transparent;
            border: none;
  
            .el-calendar__title {
              font-weight: 600;
              color: #1e293b;
              animation: slideDown 0.3s ease-out;
            }
  
            .el-calendar__header {
              border: none;
              padding: 0 0 16px 0;
            }
  
            .el-calendar__button-group {
              text-align: center;
              background: transparent;
  
              .el-button-group>.el-button {
                color: #1e293b;
                border: none;
                background: rgba(255, 255, 255, 0.1);
                transition: all 0.2s ease;
  
                &:hover {
                  background: rgba(50, 197, 255, 0.1);
                  transform: translateY(-1px);
                }
              }
            }
  
            .el-calendar-table {
              thead th {
                color: #32C5FF;
                font-size: 14px;
                font-weight: 600;
                padding: 8px 0;
              }
  
              .el-calendar-day {
                height: 40px;
                text-align: center;
                transition: all 0.2s ease;
                border-radius: 8px;
  
                &:hover {
                  background: rgba(50, 197, 255, 0.1);
                  transform: scale(1.05);
                }
              }
  
              td {
                border: none;
              }
            }
          }
        }
  
        .bot-box {
          width: 100%;
          border-radius: 16px;
          padding: 20px;
          box-sizing: border-box;
          background: #fff;
          box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
          animation: fadeIn 0.5s ease-out 0.2s backwards;
  
          .title {
            font-weight: 600;
            color: #32C5FF;
            position: relative;
            width: 100%;
            height: 60px;
            padding-bottom: 16px;
            margin-bottom: 16px;
            border-bottom: 1px solid #e2e8f0;
          }
  
          .hot-list {
            width: 100%;
            margin: 0 auto;
  
            .list-item {
              margin-bottom: 16px;
              cursor: pointer;
              display: flex;
              align-items: center;
              padding: 8px 12px;
              border-radius: 8px;
              transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
              animation: slideIn 0.3s ease-out backwards;
  
              @for $i from 1 through 10 {
                &:nth-child(#{$i}) {
                  animation-delay: #{$i * 0.1}s;
                }
              }
  
              &:hover {
                background: rgba(50, 197, 255, 0.1);
                transform: translateX(8px);
                
                .txt {
                  color: #32C5FF;
                }
              }
  
              .index-box {
                display: inline-flex;
                align-items: center;
                justify-content: center;
                width: 24px;
                height: 24px;
                font-family: PingFangSC, PingFang SC;
                font-weight: 600;
                font-size: 16px;
                color: #64748b;
                margin-right: 12px;
              }
  
              .txt {
                flex: 1;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                font-family: PingFangSC, PingFang SC;
                font-weight: 400;
                font-size: 14px;
                color: #1e293b;
                line-height: 1.5;
              }
            }
          }
        }
      }
  
      :deep(.left) {
        flex: 1;
        border: none;
        border-radius: 16px;
        overflow: hidden;
        background: #fff;
        box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
        padding: 24px;
        animation: fadeIn 0.5s ease-out 0.1s backwards;
  
        .el-form {
          margin-bottom: 24px;
        }
  
        .el-table {
          border-radius: 8px;
          overflow: hidden;
  
          th.el-table__cell {
            background-color: #f8fafc !important;
            color: #1e293b;
            font-weight: 600;
            font-size: 14px;
            padding: 12px 0;
          }
  
          td {
            height: 60px;
            padding: 12px 0;
            transition: all 0.2s ease;
  
            &:hover {
              background: rgba(50, 197, 255, 0.05);
            }
          }
  
          .el-button {
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            
            &:hover {
              transform: translateY(-2px);
              box-shadow: 0 4px 6px -1px rgba(50, 197, 255, 0.2);
            }
          }
        }
  
        .el-pagination {
          margin-top: 24px;
          justify-content: flex-end;
        }
      }
    }
  
    .el-input__wrapper {
      background-color: #f8fafc;
      border-radius: 8px;
      transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  
      &:hover, &:focus {
        background-color: #fff;
        box-shadow: 0 0 0 2px rgba(50, 197, 255, 0.2);
        transform: translateY(-1px);
      }
    }
  }
  
  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
  
  @keyframes slideIn {
    from {
      opacity: 0;
      transform: translateX(-20px);
    }
    to {
      opacity: 1;
      transform: translateX(0);
    }
  }
  
  @keyframes slideDown {
    from {
      opacity: 0;
      transform: translateY(-10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
  
  .el-table {
    th {
      word-break: break-word;
      background-color: #f8fafc !important;
      color: #1e293b;
      height: 48px;
      font-weight: 600;
      font-size: 14px;
    }
  }
  </style>
  
  <!-- npm install ace-builds@1.4.13 -->